package push;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import model.HTExtractItem;
import utils.TextUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CatM on 18/1/30.
 */
public class GTPush implements IPush {
    private static String appId = "ZTTXESu8s16ivENJa29dQ2";
    private static String appKey = "POTkzKA1oO6tRqhs6I3Ab5";
    private static String masterSecret = "hqigPlDQad5TNJiUkhGMp8";
    private static String url = "http://sdk.open.api.igexin.com/apiex.htm";


    @Override
    public void push(HTExtractItem item) {
        if(item == null) return;
        push(item.name, item.price, item.url);
    }

    @Override
    public void push(String name, String price, String linkUrl) {
        IGtPush push = new IGtPush(url, appKey, masterSecret);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        LinkTemplate template = new LinkTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle(name);
        template.setText(price);
        template.setUrl(TextUtils.isEmpty(linkUrl)? "http://getui.com" : linkUrl);

        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);

        IPushResult ret = push.pushMessageToApp(message);
        System.out.println(ret.getResponse().toString());
    }
}
